package com.alibaba._03XHR;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class Consumer {
    public static void main(String[] args) throws MQClientException {

        // 1.创建消费者
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("producer_test03_group");

        // 连接mq
        consumer.setNamesrvAddr("127.0.0.1:9876");

        // 2. 用户订阅主题，
        consumer.subscribe("test03_topic","test03_tags");

        // 3.注册监听器
        consumer.registerMessageListener((List<MessageExt> msgs, ConsumeConcurrentlyContext context) -> {
            try {
                for (MessageExt msg : msgs) {
                    String result = new String(msg.getBody(), StandardCharsets.UTF_8);
                    System.out.println(result);
                }

                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        });
        // 3.启动消费者
        consumer.start();
    }
}
